package com.fa4j.system.account.controller;

import com.fa4j.common.base.exception.CommonRequestError;
import com.fa4j.common.base.model.Page;
import com.fa4j.common.base.model.RemoveCmd;
import com.fa4j.common.web.annotation.ApiMethod;
import com.fa4j.common.web.model.AppType;
import com.fa4j.system.account.model.*;
import com.fa4j.system.account.repository.AccountRepository;
import com.fa4j.system.account.service.AccountPasswordService;
import com.fa4j.system.account.service.AccountService;
import com.fa4j.system.account.service.OperatorFillService;
import com.fa4j.system.file.service.FileUrlFillService;
import com.fa4j.system.login.service.LoginSessionService;
import com.fa4j.system.login.util.LoginUtil;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/account")
@RequiredArgsConstructor
public class AccountController {

    private final AccountService accountService;
    private final AccountPasswordService accountPasswordService;
    private final OperatorFillService operatorFillService;
    private final FileUrlFillService fileUrlFillService;
    private final LoginSessionService loginSessionService;

    private final AccountRepository accountRepository;

    @ApiMethod(name = "系统账户-分页查询", type = AppType.MANAGE)
    @GetMapping("page")
    public Page<Account> page(AccountQuery query) {
        Page<Account> result = accountRepository.queryPage(query);
        operatorFillService.fill(result.getRecords()); // 填充操作人
        return result;
    }

    @ApiMethod(name = "系统账户-详情查询", type = AppType.MANAGE)
    @GetMapping("{id}")
    @Operation(summary = "查询详情", description = "查询账户详情")
    public Account detail(@PathVariable Long id) {
        return accountRepository.findById(id)
                .map(account -> {
                    fileUrlFillService.fill(account, Account::getAvatarId, Account::setAvatarUrl);
                    return account;
                })
                .orElseThrow(() -> CommonRequestError.NOT_FOUND.build("账号"));
    }

    @ApiMethod(name = "系统账户-创建", type = AppType.MANAGE)
    @PostMapping
    public Account create(@Valid @RequestBody AccountCreateCmd cmd) {
        return accountService.create(cmd);
    }

    @ApiMethod(name = "系统账户-编辑", type = AppType.MANAGE)
    @PatchMapping
    public Account modify(@Valid @RequestBody AccountModifyCmd cmd) {
        return accountService.modify(cmd);
    }

    @ApiMethod(name = "系统账户-删除", type = AppType.MANAGE)
    @DeleteMapping("{id}")
    public Account remove(@PathVariable Long id) {
        return accountService.remove(RemoveCmd.of(id, LoginUtil.getAccountId()));
    }

    @ApiMethod(name = "系统账户-注销登录", type = AppType.MANAGE)
    @PostMapping("{id}/logout")
    public void logout(@PathVariable Long id) {
        accountRepository.findById(id).ifPresent(account -> loginSessionService.removeAccountSession(account.getId()));
    }

    @ApiMethod(name = "系统账户-重置密码", type = AppType.MANAGE)
    @PostMapping("resetPwd")
    public void resetPwd(@Valid @RequestBody AccountPwdResetCmd cmd) {
        accountPasswordService.reset(cmd);
    }


}
